package com.ikas.ai.server.module.equipment.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.equipment.dao.EquipmentPositionDAO;
import com.ikas.ai.server.module.equipment.model.po.EquipmentPosition;
import com.ikas.ai.server.module.equipment.model.vo.EquipmentPositionVo;
import com.ikas.ai.utils.CollectionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 设备部位
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/2/1 16:04
 */
@Service
public class EquipmentPositionService extends ServiceImpl<EquipmentPositionDAO, EquipmentPosition> {

    @Autowired
    private EquipmentPositionDAO equipmentPositionDAO;

    /**
     * 获取设备部位，通过设备编码
     *
     * @param equipmentCode 设备编码
     * @return list
     */
    public List<EquipmentPosition> getEquipmentPositionByEquipmentCode(String equipmentCode) {
        LambdaQueryWrapper<EquipmentPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentPosition::getEquipmentCode, equipmentCode);
        return equipmentPositionDAO.selectList(queryWrapper);
    }

    /**
     * 添加设备部位
     *
     * @param param EquipmentPosition
     * @return row
     */
    public int addEquipmentPosition(EquipmentPosition param) {
        String name = param.getName();
        String equipmentCode = param.getEquipmentCode();
        if (StringUtils.isBlank(name)
                || StringUtils.isBlank(equipmentCode) ) {
            throw new BusinessException("部位名称、设备编码不能为空");
        }
        // 同一个设备下部位名称不重复
        List<EquipmentPosition> list = getEquipmentPositionByEquipmentCode(equipmentCode);
        if (list != null && list.size() > 0) {
            list.forEach(o -> {
                if (o.getName().equals(name)) {
                    throw new BusinessException("该设备部位已经存在!");
                }
            });
        }
        param.setCreateTime(new Date());
        return equipmentPositionDAO.insert(param);
    }

    /**
     * 批量添加设备部位
     *
     * @param param EquipmentPosition
     * @return row
     */
    public int addEquipmentPositions(EquipmentPositionVo param) {
        List<String> names = param.getNames();
        String equipmentCode = param.getEquipmentCode();
        Long creator = param.getCreator();
        if (StringUtils.isBlank(equipmentCode)
                || CollectionUtil.isEmpty(names)) {
            throw new BusinessException("部位名称、设备编码不能为空");
        }

        if( isRepeat(names) ){
            throw new BusinessException("所填部位名称重复！");
        }

        // 同一个设备下部位名称不重复
        List<EquipmentPosition> list = getEquipmentPositionByEquipmentCode(equipmentCode);
        if (list != null && list.size() > 0) {
            list.forEach(o -> {
                if (names.contains(o.getName())) {
                    throw new BusinessException("该设备部位已经存在!");
                }
            });
        }

        //  批量插入
        List<EquipmentPosition> positions = new ArrayList<>();
        for (String name : names) {
            EquipmentPosition position = new EquipmentPosition();
            position.setName(name);
            position.setEquipmentCode(equipmentCode);
            position.setCreateTime(new Date());
            position.setCreator(creator);
            positions.add(position);
        }
        return equipmentPositionDAO.batchInsert(positions);
    }

    /**
     * 判断List是否有重复元素
     *
     * @param list list
     * @return 重复true
     */
    private boolean isRepeat(List<?> list){
        if( CollectionUtil.isEmpty(list) || list.size() == 1) {
            return false;
        }
        long count = list.stream().distinct().count();
        return count < list.size();
    }

    /**
     * 更新设备部位
     *
     * @param param EquipmentPosition
     * @return row
     */
    public int updateEquipmentPosition(EquipmentPosition param) {
        Long id = param.getId();
        String name = param.getName();
        String equipmentCode = param.getEquipmentCode();
        if (id == null
                ||StringUtils.isBlank(name)
                || StringUtils.isBlank(equipmentCode) ) {
            throw new BusinessException("Id、部位名称、设备编码不能为空");
        }
        // 同一个设备下部位名称不重复
        List<EquipmentPosition> list = getEquipmentPositionByEquipmentCode(equipmentCode);
        if (list != null && list.size() > 0) {
            list.forEach(o -> {
                if (o.getId().intValue() != id && o.getName().equals(name)) {
                    throw new BusinessException("该设备部位已经存在!");
                }
            });
        }
        param.setUpdateTime(new Date());
        return equipmentPositionDAO.updateById(param);
    }

    /**
     * 删除信息
     *
     * @param ids list
     */
    public int deleteEquipmentPosition(List<Long> ids) {
        return equipmentPositionDAO.deleteBatchIds(ids);
    }

    /**
     * 删除信息，通过设备信息Id
     *
     * @param equipmentCode Code
     */
    public int deleteEquipmentPositionByEquipmentCode(String equipmentCode) {
        LambdaQueryWrapper<EquipmentPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentPosition::getEquipmentCode, equipmentCode);
        return equipmentPositionDAO.delete(queryWrapper);
    }

}
